package com.joinus.server.dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.joinus.server.constant.Constant;
import com.joinus.server.entity.Meeting;
import com.joinus.server.tools.Tools;

/**
 * 活动数据库操作层<br>
 * @author
 * <ul> 
 * <li><a href="mailto:qipenghui3056@gmail.com">henrybit</a></li>
 * </ul>
 * @version 1.0
 * @since 2013-4-4
 */
public class MeetingDao extends BaseDao {
	/**活动Dao实例*/
	private final static MeetingDao meetingDao = new MeetingDao();
	/**插入SQL前缀*/
	private final static String INSERT_NEW_MEETING = "insert into meeting (id,tag,start_time,content,create_user_id,status,address,create_time,update_time) values ";
	/**查询SQL前缀*/
	private final static String QUERY_MEETING = "select * from meeting ";
	
	private MeetingDao() {
	}
	
	/**
	 * 获取MeetingDao实例<br>
	 * @return meetingDao
	 */
	public static MeetingDao getInstance() {
		return meetingDao;
	}
	
	/**
	 * 返回指定创建者的活动列表，该活动更新时间必须在updateTime之后.
	 * @param uid 创建者ID 
	 * @param updateTime 更新时间
	 * @return List-活动列表
	 */
	public List<Meeting> getMeetingList(String uid, Date updateTime) {
		List<Meeting> meetingList = new ArrayList<Meeting>();
		
		StringBuilder sql = new StringBuilder(QUERY_MEETING);
		sql.append(" where create_user_id='").append(uid).append("'");
		sql.append(" and update_time>='").append(Tools.getDateFormat(updateTime, Constant.DATE_YYYY_MM_DD_HH_MM_SS)).append("'");
		
		ResultSet rs = super.query(sql.toString());
		
		try {
			while(rs.next()) {
				long id = rs.getLong("id");
				int tag = rs.getInt("tag");
				Date startTime = rs.getDate("start_time");
				String content = rs.getString("content");
				String createUserId = rs.getString("create_user_id");
				int status = rs.getInt("status");
				String address = rs.getString("address");
				
				Meeting meeting = new Meeting(); 
				meeting.setId(id);
				meeting.setTag(tag);
				meeting.setStartTime(startTime);
				meeting.setStartTimeLong(startTime.getTime());
				meeting.setContent(content);
				meeting.setCreateUserId(createUserId);
				meeting.setStatus(status);
				meeting.setAddress(address);
				
				meetingList.add(meeting);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return meetingList;
	}
	
	/**
	 * 获取一批指定活动ID的信息列表
	 * @param meetingIdList 活动ID列表
	 * @return List-活动详细信息列表
	 */
	public List<HashMap<String, String>> getMeetingMap(List<Long> meetingIdList) {
		List<HashMap<String, String>> mapList = new ArrayList<HashMap<String, String>>();
		List<Meeting> meetingList = this.getMeetingList(meetingIdList);
		for(int i=0; meetingList!=null&&i<meetingList.size(); i++) {
			HashMap<String, String> map = new HashMap<String, String>();
			Meeting meeting = meetingList.get(i);
			map.put("id", String.valueOf(meeting.getId()));
			map.put("tag", String.valueOf(meeting.getTag()));
			map.put("start_time", String.valueOf(meeting.getStartTimeLong()));
			map.put("content", String.valueOf(meeting.getContent()));
			map.put("create_user_id", meeting.getCreateUserId());
			map.put("status", String.valueOf(meeting.getStatus()));
			map.put("address", String.valueOf(meeting.getAddress()));
			
			mapList.add(map);
		}
		return mapList;
	}
	
	/**
	 * 获取一批指定活动ID的信息列表
	 * @param meetingIdList 活动ID
	 * @return List-活动详细信息列表
	 */
	public List<Meeting> getMeetingList(List<Long> meetingIdList) {
		List<Meeting> meetingList = new ArrayList<Meeting>();
		
		StringBuilder meetingIds = new StringBuilder();
		for(int i=0; meetingIdList!=null&&i<meetingIdList.size(); i++) {
			meetingIds.append(meetingIdList.get(i));
			if(i != meetingIdList.size()-1)
				meetingIds.append(",");
		}
		
		StringBuilder sql = new StringBuilder(QUERY_MEETING);
		sql.append(" where id in(").append(meetingIds.toString()).append(")");
		
		ResultSet rs = super.query(sql.toString());
		
		try {
			while(rs.next()) {
				long id = rs.getLong("id");
				int tag = rs.getInt("tag");
				Date startTime = rs.getDate("start_time");
				String content = rs.getString("content");
				String createUserId = rs.getString("create_user_id");
				int status = rs.getInt("status");
				String address = rs.getString("address");
				
				Meeting meeting = new Meeting(); 
				meeting.setId(id);
				meeting.setTag(tag);
				meeting.setStartTime(startTime);
				meeting.setStartTimeLong(startTime.getTime());
				meeting.setContent(content);
				meeting.setCreateUserId(createUserId);
				meeting.setStatus(status);
				meeting.setAddress(address);
				
				meetingList.add(meeting);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return meetingList;
	}
	
	/**
	 * 返回指定活动ID的活动信息
	 * @param meetingId 活动ID
	 * @return meeting-活动信息
	 */
	public Meeting getMeetingById(long meetingId) {
		Meeting meeting = new Meeting();
		
		StringBuilder sql = new StringBuilder(QUERY_MEETING);
		sql.append(" where id=").append(meetingId);
		
		ResultSet rs = super.query(sql.toString());
		
		try {
			rs.next();
			long id = rs.getLong("id");
			int tag = rs.getInt("tag");
			Date startTime = rs.getDate("start_time");
			String content = rs.getString("content");
			String createUserId = rs.getString("create_user_id");
			int status = rs.getInt("status");
			String address = rs.getString("address");
			
			meeting.setId(id);
			meeting.setTag(tag);
			meeting.setStartTime(startTime);
			meeting.setStartTimeLong(startTime.getTime());
			meeting.setContent(content);
			meeting.setCreateUserId(createUserId);
			meeting.setStatus(status);
			meeting.setAddress(address);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return meeting;
	}
	
	/**
	 * 创建一条新的活动<br>
	 * @param tag 活动类型(1-购物,2-运动,3-会议,4-聚餐,5-游戏)
	 * @param startTime 活动开始时间(可以为空)
	 * @param content 活动内容(可以为空)
	 * @param createUserId 活动创建者
	 * @param status 活动状态(1-活动预备,2-活动进行中,3-活动完成,4-活动撤销)
	 * @param address 活动地址(可以为空)
	 * @return long-如果创建成功返回创建成功的主键ID;否则返回-1.
	 */
	public long createNewMessage(int tag, String startTime, String content, String createUserId, int status, String address) {
		return this.createNewMessage(tag, startTime, content, createUserId, status, address, null, null);
	}
	
	/**
	 * 创建一条新的活动<br>
	 * @param tag 活动类型(1-购物,2-运动,3-会议,4-聚餐,5-游戏)
	 * @param startTime 活动开始时间(可以为空)
	 * @param content 活动内容(可以为空)
	 * @param createUserId 活动创建者
	 * @param status 活动状态(1-活动预备,2-活动进行中,3-活动完成,4-活动撤销)
	 * @param address 活动地址(可以为空)
	 * @param createTime 创建时间
	 * @param updateTime 更新时间
	 * @return long-如果创建成功返回创建成功的主键ID;否则返回-1.
	 */
	public long createNewMessage(int tag, String startTime, String content, String createUserId, int status, String address, String createTime, String updateTime) {
		StringBuilder sql = new StringBuilder(INSERT_NEW_MEETING);
		//id auto increatement by db
		sql.append("(null");
		//tag
		sql.append(",").append(tag);
		//startTime
		if(startTime == null)
			sql.append(",").append(startTime).append("");
		else
			sql.append(",'").append(startTime).append("'");
		
		//content
		sql.append(",'").append(content).append("'");
		
		//createUserId
		sql.append(",'").append(createUserId).append("'");
		
		//status
		sql.append(",").append(status);
		
		//address
		sql.append(",'").append(address).append("'");
		
		//createTime and updateTime
		Date now = new Date();
		if(createTime == null) {
			createTime = Tools.getDateFormat(now, Constant.DATE_YYYY_MM_DD_HH_MM_SS);
		}
		if(updateTime == null) {
			updateTime = Tools.getDateFormat(now, Constant.DATE_YYYY_MM_DD_HH_MM_SS);
		}
		sql.append(",'").append(createTime).append("'");
		sql.append(",'").append(updateTime).append("'");
		
		sql.append(")");
		
		ResultSet rs = super.insert(sql.toString());
		long id = -1L;
		try {
			rs.next();
			id = rs.getLong(1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return id;
	}
}
